/*
 * @description: 
 * @Date: 2022-11-04 23:24:52
 * @LastEditTime: 2022-11-23 19:01:10
 */
import { sm2 } from "sm-crypto"
const cipherMode = 1 // 1 - C1C3C2，0 - C1C2C3，默认为1 

//公钥前面+04
let publicKey = "04af6cd7ee498c7a812018645d950d08b5e5f0f87ee672ef58b3530c84e85019c98daf606f7df331a008585ae6d2e419eedaa058d94525caabe4cedbb3b39e45b9"
let privateKey = "b2030ffcaaf7cd1a16a644ff1da4d8f40dc04faf8364da0ba31d387d90adba53"


const encode = (msgString) => {
  if (msgString == '') return msgString;
  try {
    let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果
    return encryptData;
  } catch (error) {
    throw error;
  }
}
const decode = (msgString) => {
  if (msgString == '') return msgString;
  //解密的时候如果存在04则要去掉
  if (msgString) {
    if (msgString.substr(0, 2) == '04') {
      msgString = msgString.slice(2);
    }
  }
  try {
    let decryptData = sm2.doDecrypt(msgString, privateKey, cipherMode) // 解密结果
    return decryptData;
  } catch (error) {
    throw error;
  }
}

const sm = {
  encode: encode,
  decode: decode
}
export default sm